package com.liu.yubi.utils;

import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @author lqh
 * @since 2025-05-24 15:39
 * 复制工具类
 */
public class CopyUtils {
    //单体拷贝
    /**
     *
     * @param source 资源类
     * @param target class
     */
    public static  <T> T copy(Object source, Class<T> target) {
        if (source == null) {
            return null;
        }
        T obj=null;
        try {
            //实体类
             obj = target.getConstructor ().newInstance ();
             //拷贝
             BeanUtils.copyProperties(source, obj);
        } catch (Exception e) {
            return null;
        }
        return obj;
    }

    //批量拷贝
    public static  <T> List<T> copyList(List<?> source, Class<T> target) {
        if (source == null) {
            return new ArrayList<> ();
        }
        return source.stream ().map (s->copy (s, target)).collect(Collectors.toList());
    }

}
